package com.baosight.risk.service.fltd.impl;

import com.baosight.risk.api.fltd.AuthoTypeService;
import com.baosight.risk.common.utils.SpringContextUtil;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import com.baosight.risk.service.xtzy.vo.XtzyCompanyVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service("authoTypeService")
public class AuthoTypeTemplate implements AuthoTypeService {

    @Resource(name = "compRole")
    private AuthoTypeService authoTypeService;

    @Override
    public Map<String, Object> parse(String template, Map<String, Object> param) throws Exception {
        if (template == null || "".equals(template)) {
            return authoTypeService.parse(template, param);
        }
        AuthoTypeService parser = null;
        try {
            parser = SpringContextUtil.getBean(template, AuthoTypeService.class);
        } catch (Exception e) {
            log.info("未找合适的模板解析器，将使用默认的模板解析器解析，模板：" + template);
        }
        if (parser == null) {
            return authoTypeService.parse(template, param);
        } else {
            return parser.parse(template, param);
        }
    }


    /**
     * 转换CompanyVO
     *
     * @param companyList 公司列表
     * @return 转换结果
     */
    public List<XtzyUser> convertCompanyVO(List<XtzyCompanyVO> companyList) {
        List<XtzyUser> userList = new ArrayList<>();
        if (companyList != null) {
            for (XtzyCompanyVO company : companyList) {
                if (company.getUserList() != null) {
                    for (XtzyUser companyUser : company.getUserList()) {
                        if (companyUser != null) {
                            companyUser.setExt1(company.getCompCode());
                            companyUser.setExt2(company.getCompName());
                            userList.add(companyUser);
                        }
                    }
                }
            }
        }
        return userList;
    }

    /**
     * 合并去重人员列表
     *
     * @param userList 审批人列表
     * @param newUsers 节点授权的审批人
     * @return 合并结果
     */
    public List<XtzyUser> unionUsers(List<XtzyUser> userList, List<XtzyUser> newUsers) {
        if (CollectionUtils.isNotEmpty(newUsers)) {
            Map<String, String> map = new HashMap<>();
            for (XtzyUser newUser : newUsers) {
                if (!map.containsKey(newUser.getUserCode())) {
                    userList.add(newUser);
                    map.put(newUser.getUserCode(), "");
                }
            }
        }

        return userList;
    }

    /**
     * 添加组织信息
     *
     * @param userList 审批人列表
     * @param company  当前组织
     * @return 合并结果
     */
    public List<XtzyUser> addCompany(List<XtzyUser> userList, XtzyCompanyVO company) {
        if (StringUtils.isEmpty(company.getCompCode())) {
            return userList;
        }
        if (CollectionUtils.isNotEmpty(userList)) {
            for (XtzyUser xtzyUser : userList) {
                xtzyUser.setExt1(company.getCompCode());
                xtzyUser.setExt2(company.getCompName());
            }
        }
        return userList;
    }

}
